package com.byzk.p2p.admin.user.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.user.dao.InviteRefDao;
import com.byzk.p2p.admin.user.dao.UserDao;
import com.byzk.p2p.admin.user.dto.InviteRef;
import com.byzk.p2p.admin.user.dto.User;
import com.byzk.p2p.admin.user.vo.InviteRefVo;
import com.byzk.p2p.front.dao.FrontUserDao;

@Service
public class UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private FrontUserDao frontUserDao;
	@Autowired
	private InviteRefDao inviteRefDao;
	
	public PageInfo<User> listAll(Map<String, Object> queryMap,int index,int limit){
		if (queryMap == null) {
			queryMap = new HashMap<String, Object>();
		}
		queryMap.put("start", (index-1)*limit);
		queryMap.put("limit", limit);
		int count=userDao.countAll(queryMap);
		List<User> userList=userDao.listAll(queryMap);
		PageInfo<User> page=new PageInfo<User>(count, index , limit);
		page.setPageList(userList);
		return page;
	}
	
	public Integer saveOrUpdate(User user){
		if(user.getId()==null){
			return userDao.insert(user);
		}else{
			return userDao.update(user);
		}
	}
	
	public Integer batchUpdate(List<User> users){
		return userDao.batchUpdate(users);
	}
	
	public List<User> getByUser(User user){
		return userDao.getByUser(user);
	}
	
	public User getById(Integer id){
		return userDao.getById(id);
	}
	
	public User getCodeById(Integer id){
		return userDao.getCodeById(id);
	}
	
	public Integer countAll(){
		return userDao.getAll();
	}
	
	public Integer countAll(Map<String, Object> queryMap){
		return userDao.countAll(queryMap);
	}
	
	public Integer deleteUser(Integer id){
		return userDao.delete(id);
	}
	
	public User selectByNameOrPhone(String username){
		return frontUserDao.selectByNameOrPhone(username);
	}
	
	/**
	 * 根据内部邀请码获取用户
	 * @param insideInviteCode 内部邀请码
	 * @return
	 */
	public List<User> getUserByInviteCode(String insideInviteCode){
		return userDao.getUserByInsideInviteCode(insideInviteCode);
	}
	
	/**
	 * 获取所有的邀请用户
	 * @param vo
	 * @return
	 */
	public PageInfo<InviteRefVo> listInviteRef(InviteRefVo vo){
		int count=inviteRefDao.inviteCount(vo);
		List<InviteRefVo> list =inviteRefDao.listInvite(vo);
		PageInfo<InviteRefVo> page=new PageInfo<InviteRefVo>(count,vo.getIndex() ,vo.getLimit());
		page.setPageList(list);
		return page;
	}
	
	/**
	 * 获取邀请用户排名
	 * @param vo
	 * @return
	 */
	public PageInfo<InviteRefVo> listInviteUserRank(InviteRefVo vo){
		int count=inviteRefDao.inviteRankCount(vo);
		List<InviteRefVo> list =inviteRefDao.listInviteRank(vo);
		PageInfo<InviteRefVo> page=new PageInfo<InviteRefVo>(count,vo.getIndex() ,vo.getLimit());
		page.setPageList(list);
		return page;
	}
	
	public User getUserByPIpsAcctNo(String pIpsAcctNo){
		return userDao.getUserByPIpsAcctNo(pIpsAcctNo);
	}
	
	public Integer userAll(User user){
		return userDao.userAll(user);
	}
	
	public User getUserByPhone(String phone){
		return userDao.getUserByPhone(phone);
	}	
	
	public List<User> listAll1(Map<String, Object> queryMap) {
		return userDao.listAll1(queryMap);
	}
	
	/**
	 * 获取一个用户领取过对应等级的奖励
	 * @param userId 用户ID
	 * @param level 对应会员等级
	 * @return
	 */
	public InviteRef getInviteRefByLevel(Integer userId,String level) {
		return inviteRefDao.getInviteRefByLevel(userId, level);
	}
	
	/**
	 * 根据被邀请人用户ID，获取邀请关系。
	 * @return
	 */
	public InviteRef getInviteRefByUserId(Integer userId){
		return inviteRefDao.getInviteRefByUserId(userId);
	}

	public List<InviteRefVo> getInviteRefByPid(Integer pid){
		return inviteRefDao.getInviteRefByPid(pid);
	}
	/**
	 * 更新用户关联信息
	 * @param inviteRef
	 * @return
	 */
	public int updateInviteRef(InviteRef inviteRef) {
		return inviteRefDao.update(inviteRef);
	}
	
	/**
	 * 建立邀请关系
	 * @param inviteRef
	 * @return
	 */
	public Integer saveOrUpdateInviteRef(InviteRef inviteRef){
		if (inviteRef.getId() == null) {
			return inviteRefDao.insert(inviteRef);
		}else {
			return inviteRefDao.update(inviteRef);
		}
	}
	
	/**
	 * Description:根据用户名查找用户
	 * @auther:fdq
	 * @date :2016年10月20日
	 */
	public User getByName(String username){
		return userDao.getByName(username);
	}
	
}
